{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "248fde87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "郑周长为20\n"
     ]
    }
   ],
   "source": [
    "class shape(object):\n",
    "    def __init__(self,name):\n",
    "        super().__init__()\n",
    "        self.name=name\n",
    "\n",
    "\n",
    "    def zhouchang(d):\n",
    "        pass\n",
    "class Square(shape):\n",
    "    def __init__(self,name,d):\n",
    "        super().__init__(name)\n",
    "        self.d=d\n",
    "\n",
    "    def zhouchang(self):\n",
    "        print(f\"{self.name}周长为{4*self.d}\")\n",
    "a=Square(\"郑\",5)\n",
    "a.zhouchang()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "795d4413",
   "metadata": {},
   "outputs": [],
   "source": [
    "#激活函数\n",
    "import numpy as np\n",
    "data=np.ones(5,5)\n",
    "\n",
    "class activation():\n",
    "    def sigmoid(x):\n",
    "        return 1/(1+np.exp(-x))\n",
    "    def relu(x):\n",
    "        return np.maxium(0,x)\n",
    "    def tanh(x):\n",
    "        return np.tanh(x)\n",
    "#交叉熵损失函数\n",
    "class CrossEntropyLoss:\n",
    "    def binary_cross_entropy(y_true,y_pred):\n",
    "        epsilon=1e-15\n",
    "        y_pred=np.clip(y_pred,epsilon,1-epsilon)\n",
    "        return -np.mean(y_true*np.log(y_pred)+(1-y_true)*np.log(1-y_pred))\n",
    "    def category_cross_entropy(y_true,y_pred):\n",
    "        epsilon=1e-15\n",
    "        y_pred=np.clip(y_pred,epsilon,1-epsilon)\n",
    "        return -np.mean(np.sum(y_true*np.log(y_pred),axis=1))\n",
    "\n",
    "\n",
    "\n",
    "#感知机\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "\n",
    "class perception():\n",
    "    def __init__(self):\n",
    "        self.w=np.ones(len(data[0])-1,dtype=np.float32)#一般减去标签列\n",
    "        self.b=0\n",
    "        self.rate=0.5\n",
    "    \n",
    "    def fit(self,xtrain,ytrain):\n",
    "        while True:\n",
    "            flag=True\n",
    "            for i in range(len(xtrain)):\n",
    "                xi=xtrain[i]\n",
    "                yi=ytrain[i]\n",
    "                #np.inner()用于计算两个数组的点积\n",
    "                if yi*(np.inner(self.w,xi)+self.b)<=0:\n",
    "                    flag=False\n",
    "                    self.w += self.rate*np.dot(xi,yi)\n",
    "                    self.b +=self.rate*yi\n",
    "                if flag:break\n",
    "        print(\"w=\"+str(self.w)+\",b=\"+str(self.b))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e788a742",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "class cnn(nn.Module):\n",
    "    def __init__(self,num_class=10):\n",
    "        self.conv1=nn.Conv2d(in_channels=1,out_channels=16,kernel_size=3,stride=2)\n",
    "        self.bn1=nn.BatchNorm2d(16)\n",
    "        self.relu1=nn.ReLU()\n",
    "        self.pool1=nn.Maxpool2d(2,2)\n",
    "        self.linear1=nn.Linear(16*8*8,256)\n",
    "        self.linear=nn.Linear(256,num_class)\n",
    "\n",
    "    def forward(self,x):\n",
    "        #原图32*32\n",
    "        x=self.pool1(self.relu1(self.bn1(self.conv1(x))))\n",
    "\n",
    "        x=nn.Flatten(x)\n",
    "        x=self.linear1(x)\n",
    "        x=self.relu1(x)\n",
    "        x=self.linear2(x)\n",
    "        return x"
   ]
  },
  {
   "metadata": {
    "jupyter": {
     "is_executing": true
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "\n",
    "class cnn(nn.Module):\n",
    "    def __init__(self,in_channel,out_channel):\n",
    "        super().__init__()\n",
    "        self.conv1=nn.Conv2d(in_channels=1,out_channels=16,kernel_size=3,stride=2)\n",
    "        self.bn1=nn.BatchNorm2d(16)\n",
    "        self.relu1=nn.ReLU()\n",
    "\n",
    "        self.linear1=nn.Linear(16*2*2,2)\n",
    "    def forward(self,x):\n",
    "        x=self.conv1(x)\n",
    "        x=self.bn1(x)\n",
    "        x=self.relu1(x)\n",
    "\n",
    "        x=nn.Flatten()\n",
    "        x=self.linear(x)\n",
    "        return x\n"
   ],
   "id": "8de47949aeaa259e",
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b58221cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "\n",
    "class lstmmodel(nn.Module):\n",
    "    def __init__(self,input_size,hidden_size,output_size,num_layers=1):\n",
    "        super().__init__()\n",
    "        self.hidden_size=hidden_size\n",
    "        self.num_layers=num_layers\n",
    "        self.lstm=nn.LSTM(input_size,hidden_size,num_layers,batch_first=True)\n",
    "        self.fc=nn.Linear(hidden_size,output_size)\n",
    "def forward(self,x):\n",
    "    h0=torch.zeros(self.num_layers,x.size(0),self.hidden_size).to(x.device)\n",
    "    c0=torch.zeros(self.num_layers,x.size(0),self.hidden_size).to(x.device)\n",
    "    out,_=self.lstm(x,(h0,c0))\n",
    "    out=self.fc(out[:,-1,:])\n",
    "    return out\n"
   ]
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "class perception():\n",
    "    def __init__(self):\n",
    "        self.w=np.ones(len(data[0]))\n",
    "        self.b=0\n",
    "        self.rate=0.5\n",
    "    def fit(self,x_train,y_train):\n",
    "        while True:\n",
    "            flag=True\n",
    "            for i in range(len(xtrain))"
   ],
   "id": "8116768a949980d7"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
